const app = getApp();

// pages/cart/cart.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hide: app.globalData.hide,
    checked: false,
    allPrice: 0,
    cartList: [
      {
        id: 1,
        title: "商品1",
        detail: "规格：256G 白色",
        checked: true,
        goodsImg: "/static/images/goods.png",
        num: 1,
        price: 9999,
      },
      {
        id: 2,
        title: "商品2",
        detail: "规格：16+1T 银色",
        checked: false,
        goodsImg: "/static/images/goods1.jpg",
        num: 2,
        price: 8888,
      },
    ],
  },

  onClose(event) {
    const position = event.detail;
    switch (position) {
      case "left":
      case "cell":
        // instance.close();
        break;
      case "right":
        wx.showModal({
          title: "删除提醒",
          content: "确定从购物车中删除该商品吗？",
          confirmColor: "#ff2121",
          success: (result) => {
            if (result.confirm) {
              // instance.close();
            }
          },
          fail: () => { },
          complete: () => {
            // instance.close();
          },
        });
        break;
    }
  },

  onChange(e) {
    let { cartList, checked } = this.data;
    const id = e.currentTarget.dataset.id;
    cartList.forEach((i) => {
      if (i.id == id) {
        i.checked = !i.checked;
      }
    });

    checked = cartList.every((i) => i.checked);

    this.setData({
      cartList,
      checked,
    });

    this.count();
  },

  checkAll() {
    let { checked, cartList } = this.data;
    checked = !checked;
    cartList.forEach((i) => (i.checked = checked));
    this.setData({
      checked,
      cartList,
    });

    this.count();
  },

  onClickButton() {
    const { allPrice } = this.data;
    if (allPrice * 100 == 0) {
      wx.showModal({
        title: "提交失败",
        content: "请选择商品后再提交订单",
        showCancel: false,
        complete: (res) => {
          if (res.confirm) {
          }
        },
      });
      return;
    }
    wx.navigateTo({
      url: "/pages/order/create/create",
    });
  },

  numChange(e) {
    const { cartList } = this.data;
    const num = e.detail;
    const { id } = e.currentTarget.dataset;
    cartList.forEach((i) => {
      if (i.id == id) {
        i.num = num;
      }
    });
    this.setData({
      cartList,
    });
    this.count();
  },

  count() {
    let { cartList, allPrice } = this.data;
    // console.log(cartList);
    allPrice = 0;
    cartList.forEach((i) => {
      if (i.checked) {
        allPrice += i.price * i.num;
      }
    });
    this.setData({
      allPrice,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2,
      });
    }

    this.count();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
